﻿using System;
using System.Collections.Concurrent;

namespace YWM.EntityFrameworkCore.Extensions
{
    /// <summary>
    /// 常量、公共方法配置类
    /// </summary>
    internal static class Penetrates
    {
        /// <summary>
        /// 数据库上下文描述器
        /// </summary>
        internal static readonly ConcurrentDictionary<string, Type> DbContextDescriptors;

        /// <summary>
        /// 构造函数
        /// </summary>
        static Penetrates()
        {
            DbContextDescriptors = new ConcurrentDictionary<string, Type>();
        }

        /// <summary>
        /// 检查数据库上下文是否绑定
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dbContextType"></param>
        /// <returns></returns>
        internal static void CheckDbContext(string key, out Type dbContextType)
        {
            if (!DbContextDescriptors.TryGetValue(key, out dbContextType)) throw new InvalidCastException($" The dbcontext `{key}` is not bind.");
        }
    }
}